package gr.unipi.mscd1;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class GeneralInfo extends SnmpInfo implements SnmpValuesListener {

	public MainForm mform;
	private List<OidInfo> oids;
	
	public GeneralInfo(SimpleSnmpClient client, final MainForm mform) {
		super(client);
				
		this.mform = mform;
		
		oids = new ArrayList<OidInfo>() {
			private static final long serialVersionUID = 1L;

			{
				add(new OidInfo(OidConsts.sysName, ParamType.STRING, mform.txtSysName));
				add(new OidInfo(OidConsts.sysDescr, ParamType.STRING, mform.txtSysDescr));
				add(new OidInfo(OidConsts.sysLocation, ParamType.STRING, mform.txtSysLocation));
				add(new OidInfo(OidConsts.sysContact, ParamType.STRING, mform.txtSysContact));
				add(new OidInfo(OidConsts.sysUpTime, ParamType.STRING, mform.txtSysUpTime));
				add(new OidInfo(OidConsts.ifNumber, ParamType.INT, mform.txtIfCount));
				add(new OidInfo(OidConsts.tcpInSegs, ParamType.LONG, mform.txtSegIn));
				add(new OidInfo(OidConsts.tcpInErrs, ParamType.LONG, mform.txtErrsIn));
				add(new OidInfo(OidConsts.tcpRetransSegs, ParamType.LONG, mform.txtTcpRetrans));
				add(new OidInfo(OidConsts.tcpRtoAlgorithm, ParamType.INT, mform.txtAlg));
				add(new OidInfo(OidConsts.tcpRtoMax, ParamType.INT, mform.txtTimeoutMax));
				add(new OidInfo(OidConsts.tcpRtoMin, ParamType.INT, mform.txtTimeoutMin));
				add(new OidInfo(OidConsts.tcpCurrEstab, ParamType.LONG, mform.txtEstNo));
			}
		};
		
		this.addSnmpValueListener(this);
		
		initialize();
	}

	@Override
	protected List<OidInfo> getOids() {
		return oids;
	}

	@Override
	public void valuesRetrieved(Object sender) {
		long sumTcpIn = Long.parseLong(mform.txtSegIn.getText());
		DecimalFormat df = new DecimalFormat("###.##");
		
		long errsIn = Long.parseLong(mform.txtErrsIn.getText());
		
		double errsPerc = ((double)errsIn) / ((double)sumTcpIn);
		
		long retrans = Long.parseLong(mform.txtTcpRetrans.getText());
		
		double retransPerc = ((double)retrans) / ((double)sumTcpIn);
		
		double maxTimeout = Double.parseDouble(mform.txtTimeoutMax.getText()) / (double)1000;
		
		double minTimeout = Double.parseDouble(mform.txtTimeoutMin.getText()) / (double)1000;
		
		int algoId = Integer.parseInt(mform.txtAlg.getText());
		String alDescr = algoDescr.get(algoId);
		if (alDescr == null) {
			alDescr = algoId + "";
		}
		
		mform.txtErrsIn.setText(mform.txtErrsIn.getText() + " (" + df.format(errsPerc) + "%)");
		
		mform.txtTcpRetrans.setText(mform.txtTcpRetrans.getText() + " (" + df.format(retransPerc) + "%)");
		
		mform.txtTimeoutMax.setText(df.format(maxTimeout));
		
		mform.txtTimeoutMin.setText(df.format(minTimeout));
		
		mform.txtAlg.setText(alDescr + "(" + algoId + ")"); 
		
	}

}
